package com.atguigu.controller;


import com.atguigu.entity.BaseBrand;
import com.atguigu.entity.BaseSaleProperty;
import com.atguigu.entity.ProductSpu;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseSalePropertyService;
import com.atguigu.service.ProductSpuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author GuoJH
 * @since 2021-08-24
 */
@RestController
@RequestMapping("/product")
public class ProductSpuController {
    @Autowired
    private ProductSpuService productSpuService;
    @Autowired
    private BaseSalePropertyService salePropertyService;

    //根据id获取spu的信息
    @GetMapping("/queryProductSpuByPage/{current}/{limit}/{category3Id}")
    public RetVal queryProductSpuByPage(@PathVariable Long current,
                                        @PathVariable Long limit,
                                        @PathVariable Long category3Id){
        Page<ProductSpu> productSpuPage = new Page<>(current, limit);
        QueryWrapper<ProductSpu> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id",category3Id);
        //可做个排序
        wrapper.orderByDesc("id");
        productSpuService.page(productSpuPage, wrapper);
        return RetVal.ok(productSpuPage);
    }
    //获取商品属性的信息
    @GetMapping("/queryAllSaleProperty")
    public RetVal queryAllSaleProperty(){
        List<BaseSaleProperty> list = salePropertyService.list(null);
        return RetVal.ok(list);
    }
    //保存商品SPU信息
    @PostMapping("/saveProductSpu")
    public RetVal saveProductSpu(@RequestBody ProductSpu productSpu){
        productSpuService.saveProductSpu(productSpu);
        return RetVal.ok();
    }

}

